package com.cheng.spring04.dao.impl;

import com.cheng.spring04.dao.BookDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {
    private final JdbcTemplate jdbcTemplate;

    public BookDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public Integer queryBookPriceById(int bookId) {
        String sql = "select price from t_books2 where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, bookId);
    }

    @Override
    public Integer queryBookStockById(int bookId) {
        String sql = "select stock from t_books2 where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, bookId);
    }

    @Override
    public void updateBookStockById(int bookId, int num) {
        String sql = "update t_books2 set stock = stock - ? where id = ?";
        jdbcTemplate.update(sql, num, bookId);
    }
}
